import { createRouter, createWebHistory } from 'vue-router'
//系统基本功能组件导入
import LoginView from "@/views/system/LoginView.vue";
import RegisterView from "@/views/system/RegisterView.vue";
import ForgotPwdView from "@/views/system/ForgotPwdView.vue";
//游客组件导入
import VisitorCommunal from "@/components/communal/visitor-communal.vue";
import VisitorHomeView from "@/views/visitor/VisitorHomeView.vue";
import visitorProcedureView from "@/views/visitor/VisitorProcedureView.vue";
import VisitorModelTrialView from "@/views/visitor/VisitorModelTrialView.vue";
//数据统计组件导入
import ScreenCommunal from "@/components/communal/screen-communal.vue";
import ScreenHomeView from "@/views/screen/ScreenHomeView.vue";
import ScreenStatisticView from "@/views/screen/ScreenStatisticView.vue";
//响应结果页导入
import _403View from "@/views/service/403View.vue"
import _404View from "@/views/service/404View.vue"
import _500View from "@/views/service/500View.vue"


//创建路由
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      name:'main',
      redirect: '/visitor/home'
    },
    {
      path:'/login',
      name:'login',
      component:LoginView
    },
    {
      path:'/forgot',
      name:'forgot',
      component:ForgotPwdView
    },
    {
      path:'/register',
      name:'register',
      component:RegisterView
    },
    {
      path:'/visitors',
      name:'visitors',
      redirect:'/visitor/home',
      children:[
        {
          path: '/visitor/home',
          name: 'visitor/home',
          component: VisitorHomeView
        },
        {
          path:'/visitor/procedure',
          name:'visitor/procedure',
          component:visitorProcedureView
        },
        {
          path:'/visitor/modelTrial',
          name:'visitor/modelTrial',
          component:VisitorModelTrialView
        }
      ]
    },
    {
      path:'/screens',
      name:'screens',
      redirect: '/screen/home',
      children:[
        {
          path: '/screen/home',
          name: 'screen/home',
          component: ScreenHomeView
        },
        {
          path: '/screen/statistic',
          name: 'screen/statistic',
          component: ScreenStatisticView
        },
      ]
    },
    {
      path:'/403',
      name:'403',
      component:_403View
    },
    {
      path:'/404',
      name:'404',
      component:_404View
    },
    {
      path:'/500',
      name:'500',
      component:_500View
    }
  ]
})
//将不同权限访问页面进行分流处理
let screenRoutes = []  //管理访问路由
let visitorRoutes = []   //用户访问路由
let serverRoutes = []   //服务响应路由
router.getRoutes().forEach((rt)=>{
  //设置管理员路由，以screen开始
  if (rt.name.slice(0,6)==='screen'){
    screenRoutes.push(rt.name)
  }
  //设置游客路由，以user开始
  else if (rt.name.slice(0,7)==='visitor'){
    visitorRoutes.push(rt.name)
  }
  //设置响应服务路由，无指定字符串起始
  else {
    serverRoutes.push(rt.name)
  }
})
//对路由进行校验
function routerFilter(paths=[],name) {
  let action = false;
  paths.forEach((path)=>{
    if (name===path){
      action=true
    }
  })
  return action
}
router.beforeEach((to,from,next) => {
  //如果用户不存在，则无法访问screen页面
  const screenStr = JSON.parse(sessionStorage.getItem('user'))
  if(routerFilter(screenRoutes,to.name) && !screenStr){
    $message.destroyAll()
    $message.error("- 请先登录 -")
    return next(from.path)
  }
  if(routerFilter(screenRoutes,to.name) && screenStr) return next()
  if (routerFilter(visitorRoutes,to.name)) return next()
  if (routerFilter(serverRoutes,to.name)) return next()
  return next("/404")
})

export default router
